﻿using System;
using System.Windows.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;

namespace Padsurface.App.UserControls
{
    public sealed partial class TempoUserControl : UserControl
    {
        public event EventHandler<TappedRoutedEventArgs> TempoIncreased;
        public event EventHandler<HoldingRoutedEventArgs> TempoIncreasing;

        public event EventHandler<TappedRoutedEventArgs> TempoDecreased;
        public event EventHandler<HoldingRoutedEventArgs> TempoDecreasing;

        #region BpmProperty

        public string Bpm
        {
            get { return (string)GetValue(BpmProperty); }
            set { SetValue(BpmProperty, value); }
        }

        public static readonly DependencyProperty BpmProperty =
            DependencyProperty.Register("Bpm", typeof(string), typeof(TempoUserControl), new PropertyMetadata("0"));

        #endregion

        #region IncreaseTempoProperty

        public ICommand IncreaseTempoCommand
        {
            get { return (ICommand)GetValue(IncreaseTempoCommandProperty); }
            set { SetValue(IncreaseTempoCommandProperty, value); }
        }

        public static readonly DependencyProperty IncreaseTempoCommandProperty =
            DependencyProperty.Register("IncreaseTempoCommand", typeof(ICommand), typeof(TempoUserControl), new PropertyMetadata(null));

        #endregion

        #region DecreaseTempoProperty

        public ICommand DecreaseTempoCommand
        {
            get { return (ICommand)GetValue(DecreaseTempoCommandProperty); }
            set { SetValue(DecreaseTempoCommandProperty, value); }
        }

        public static readonly DependencyProperty DecreaseTempoCommandProperty =
            DependencyProperty.Register("DecreaseTempoCommand", typeof(ICommand), typeof(TempoUserControl), new PropertyMetadata(null));

        #endregion

        public bool IsIncreaseTempoEnabled 
        {
            get { return increaseButton.IsEnabled; }
            set { increaseButton.IsEnabled = value; }
        }

        public bool IsDecreaseTempoEnabled 
        {
            get { return decreaseButton.IsEnabled; }
            set { decreaseButton.IsEnabled = value; } 
        }

        public TempoUserControl()
        {
            this.InitializeComponent();
        }

        private void IncreaseButtonTapped(object sender, TappedRoutedEventArgs e)
        {
            if (TempoIncreased != null)
                TempoIncreased(sender, e);
        }

        private void DecreaseButtonTapped(object sender, TappedRoutedEventArgs e)
        {
            if (TempoDecreased != null)
                TempoDecreased(sender, e);
        }

        private void IncreaseButtonHolding(object sender, HoldingRoutedEventArgs e)
        {
            if (TempoIncreasing != null)
                TempoIncreasing(sender, e);
        }

        private void DecreaseButtonHolding(object sender, HoldingRoutedEventArgs e)
        {
            if (TempoDecreasing != null)
                TempoDecreasing(sender, e);
        }
    }
}
